//
//  MenuDarenZuopinViewController.m
//  Products_B
//
//  Created by lanou on 16/6/3.
//  Copyright © 2016年 朋学友. All rights reserved.
//
// http://api.izhangchu.com/

  //  从 上个界面传id
// methodName=ShequPostview&post_id=28893&token=&user_id=&version=4.4
// methodName=ShequPostview&post_id=49327&token=&user_id=&version=4.4
//  评论 methodName=CommentList&page=1&relate_id=49327&size=10&token=&type=3&user_id=&version=4.4
#import "MenuDarenZuopinViewController.h"
#import "MenuDarenZuoPIngModel.h"
#import "MenuDarenZuoPingCollectionViewCell.h"
#import "MenuDarenPinglunModel.h"
#import "MenuDarenZuoPingTableViewCell.h"
#import "MenuDarenZuoPingCollectionViewCell.h"
#import "MenuDaren2CollectionViewCell.h"
#import "MenuDarenHeaderCollectionReusableView.h"
#import "MenuDarenFootCollectionReusableView.h"
#import "MenuDarenHeader2CollectionReusableView.h"
#import "MenuDaren3CollectionViewCell.h"
@interface MenuDarenZuopinViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>




@property (nonatomic, strong)NSMutableDictionary *parDic;

@property (nonatomic, strong)NSMutableArray *pingLunModelArray;
@property (nonatomic, strong)MenuDarenZuoPIngModel *model;
@property (nonatomic, strong)UIButton *peoButton;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, strong)UIView *myView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UICollectionView *smallCollectionView;
@end

@implementation MenuDarenZuopinViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pingLunModelArray = [NSMutableArray array];
//    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 300)];
    
    
//    [self.view addSubview:_headView];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[MenuDaren2CollectionViewCell class] forCellWithReuseIdentifier:@"MenuDaren2CollectionViewCell"];
    [self.collectionView registerClass:[MenuDarenZuoPingCollectionViewCell class] forCellWithReuseIdentifier:@"MenuDarenZuoPingCollectionViewCell"];
    [self.collectionView registerClass:[MenuDarenHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuDarenHeaderCollectionReusableView"];
    [self.collectionView registerClass:[MenuDarenFootCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MenuDarenFootCollectionReusableView"];
    [self.collectionView registerClass:[MenuDarenHeader2CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuDarenHeader2CollectionReusableView"];
    
    [self.view addSubview:_collectionView];
    
    self.peoButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.peoButton.frame = CGRectMake(kWidth - 50, 335, 30, 30);
    [self.peoButton setBackgroundImage:[UIImage imageNamed:@"menutiaozhuan"] forState:(UIControlStateNormal)];
    [self.peoButton addTarget:self action:@selector(peoButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.peoButton.backgroundColor = [UIColor redColor];
    [self.collectionView addSubview:_peoButton];
    
    self.parDic = @{@"methodName":@"CommentList",@"page":@"1",@"relate_id":@"49327",@"size":@"10",@"token":@"",@"type":@"3",@"user_id":@"",@"version":@"4.4"}.mutableCopy;
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    NSLog(@"%@",self.post_id);
    
    
    [self requestData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   
        
    
//    MenuDarenZuoPIngModel *model = self.model;
    if (section == 0) {

        return 5;
    }
//        return model.zuoPingArray.count;
    return 1;
    }

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"aa");
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MenuDarenZuoPingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuDarenZuoPingCollectionViewCell" forIndexPath:indexPath];
        
        MenuDarenZuoPIngModel *model = self.model;
            cell.model = model.peoArray[indexPath.row];
            
        
    
        return cell;
    }
  
    MenuDaren2CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuDaren2CollectionViewCell" forIndexPath:indexPath];
    
    
    MenuDarenZuoPIngModel *model = self.model;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  
    
//        cell.model = model.zuoPingArray[indexPath.row];
//    cell.collectionView.contentSize = CGSizeMake(kWidth / 3 *model.zuoPingArray.count, kWidth / 3);
//    [cell.collectionView registerClass:[MenuDaren3CollectionViewCell class] forCellWithReuseIdentifier:@"MenuDaren3CollectionViewCell"];
//    cell.collectionView.delegate = self;
//    cell.collectionView.dataSource = self;
    
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        if (indexPath.section == 0) {
            return CGSizeMake(50, 50);
        }
        return CGSizeMake(kWidth, kWidth / 3);
    }





- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    //   第一个区头
    if (indexPath.section == 0) {
        if (kind == UICollectionElementKindSectionHeader) {
    
            MenuDarenHeaderCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuDarenHeaderCollectionReusableView" forIndexPath:indexPath];
            
            
         
            headView.model = _model;
               return headView;
            
          }
    
    }
//    return nil;
//
//

//    //  区尾   tableView
    if (kind == UICollectionElementKindSectionFooter) {
         MenuDarenFootCollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MenuDarenFootCollectionReusableView" forIndexPath:indexPath];
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, self.pingLunModelArray.count *100 + 60)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[MenuDarenZuoPingTableViewCell class] forCellReuseIdentifier:@"MenuDarenZuoPingTableViewCell"];
        self.tableView.rowHeight = 100;
        self.tableView.bounces = NO;
        self.tableView.scrollEnabled = NO;
        [footView addSubview:_tableView];
    return footView;
    }
//    //  第二分区区头
    if (indexPath.section == 1) {
        if (kind == UICollectionElementKindSectionHeader) {
            MenuDarenHeader2CollectionReusableView *myHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MenuDarenHeader2CollectionReusableView" forIndexPath:indexPath];
            

            myHeadView.model = self.model;

            
 
            return myHeadView;
            }
            
    
        }
    

    return nil;

}












- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    if (section == 0) {
//        return CGSizeMake(kWidth, 330);
//    }
//    return CGSizeMake(kWidth, 30);
    if (section == 0) {
        return CGSizeMake(kWidth, 330);
    }
    return  CGSizeMake(kWidth, 30);
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 1) {
        
        return CGSizeMake(kWidth, 0 + self.pingLunModelArray.count *100 + 60);
        
    }
    return CGSizeMake(0, 0);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.pingLunModelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuDarenZuoPingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuDarenZuoPingTableViewCell" forIndexPath:indexPath];
    MenuDarenPinglunModel *model = self.pingLunModelArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 60)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 20)];
    label.font = [UIFont systemFontOfSize:12];
    
    label.text = [NSString stringWithFormat:@"%ld条评论",self.pingLunModelArray.count];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(10, 30, kWidth - 20, 30);
    [button setBackgroundColor: [UIColor orangeColor]];
    [button setTitle:@"我也说两句" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(shuoButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [myView addSubview:label];
    [myView addSubview:button];
//    myView.backgroundColor = [UIColor redColor];
    return myView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
#pragma mark - 更多粉丝的按钮 
- (void)peoButtonAction:(UIButton *)button{
    
}
#pragma mark - 我也说两句 Button - 
- (void)shuoButtonAction:(UIButton *)button{
    
}
#pragma mark - 更多地点击方法 
- (void)moreButtonAction:(UIButton *)button{
    
}
#pragma mark - 赞 -
- (void)zanButtonAction:(UIButton *)button{
    
}
#pragma mark - 关注 -
- (void)guanzhuButtonAction:(UIButton *)button{
    
}
- (void)requestData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:@{@"methodName":@"ShequPostview",@"post_id":self.post_id,@"token":@"",@"user_id":@"",@"version":@"4.4"} finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
        NSLog(@"%@",jsonDic[@"data"][@"content"]);
        self.model =  [MenuDarenZuoPIngModel modelConfigureWithDic:jsonDic];
    } error:nil];
    
    
    
    
    
    
    
    
    //  评论
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
        self.pingLunModelArray = [MenuDarenPinglunModel modelConfigureWith:jsonDic];
        
        [self.collectionView reloadData];
    } error:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
